<template>
	<view class="yhquan">
		<u-sticky bgColor="#fff">
			<u-tabs
				:current="current"
				:scrollable="tabNav.length > 5"
				:list="tabNav"
				@click="clickTab"
				inactiveStyle="font-size: 28rpx;"
				lineColor="#E54E4E"
				itemStyle="height: 88rpx;"
			></u-tabs>
		</u-sticky>

		<view class="container">
			<quanlist :list="list" @updataUnfold="updataUnfold"></quanlist>
			<u-loadmore :status="status" nomoreText=" " />

			<u-empty :show="empty" mode="car" icon="/static/images/empty01.png" text="暂无优惠券">
				<button v-if="empty && current === 0" @click="toNav(`quan/quan`)" style="margin-top: 40rpx; border-radius: 40rpx; width: 400rpx; font-size: 28rpx">去领取</button>
			</u-empty>
		</view>
	</view>
</template>

<script>
export default {
	name: 'yhquan',

	data() {
		return {
			current: 0,
			status: '',
			tabNav: [
				{
					name: '未使用'
				},
				{
					name: '已使用'
				},
				{
					name: '已过期'
				}
			],
			list: [],
			empty: false,
			paging: {
				page: 1,
				pagenum: 10
			}
		};
	},
	onLoad() {
		this.reset();
		this.init();
	},
	onReachBottom() {
		if (this.status === 'nomore') return;
		this.init(this.current);
	},
	methods: {
		updataUnfold(e) {
			let { a, i } = e;
			this.list[i].unfold = a;
		},
		init() {
			this.empty = false;
			this.status = 'loading';
			this.$http.getJson('users_myYhqList', { scene: this.current + 1, ...this.paging }).then((res) => {
				const { code, data, message } = res;
				console.log(res);
				if (code === 1) {
					data.forEach((item) => {
						item.unfold = false;
					});
					if (data.length !== 0) this.list = this.list.concat(data);
					if (this.paging.pagenum === data.length) {
						this.paging.page += 1;
						this.status = 'loadmore';
					} else {
						this.status = 'nomore';
					}
					this.empty = !this.list.length;
				}
			});
		},

		reset() {
			this.paging.page = 1;
			this.list = [];
		},
		clickTab(e) {
			this.reset();
			this.current = e.index;
			this.init();
		}
	}
};
</script>

<style lang="less" scoped>
.yhquan {
	.container {
		padding: 24rpx 32rpx;
	}
}
</style>
